package cn.iocoder.yudao.module.blog.service.space;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.blog.controller.admin.space.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.space.BlogSpaceDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.blog.convert.space.BlogSpaceConvert;
import cn.iocoder.yudao.module.blog.dal.mysql.space.BlogSpaceMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.blog.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 动态 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class BlogSpaceServiceImpl implements BlogSpaceService {

    @Resource
    private BlogSpaceMapper spaceMapper;

    @Override
    public Long createSpace(BlogSpaceCreateReqVO createReqVO) {
        // 插入
        BlogSpaceDO space = BlogSpaceConvert.INSTANCE.convert(createReqVO);
        spaceMapper.insert(space);
        // 返回

        return space.getId();
    }

    @Override
    public void updateSpace(BlogSpaceUpdateReqVO updateReqVO) {
        // 校验存在
        validateSpaceExists(updateReqVO.getId());
        // 更新
        BlogSpaceDO updateObj = BlogSpaceConvert.INSTANCE.convert(updateReqVO);
        spaceMapper.updateById(updateObj);
    }

    @Override
    public void deleteSpace(Long id) {
        // 校验存在
        validateSpaceExists(id);
        // 删除
        spaceMapper.deleteById(id);
    }

    private void validateSpaceExists(Long id) {
        if (spaceMapper.selectById(id) == null) {
            throw exception(SPACE_NOT_EXISTS);
        }
    }

    @Override
    public BlogSpaceDO getSpace(Long id) {
        return spaceMapper.selectById(id);
    }
    @Override
    public List<BlogSpaceDO> getSpaceList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return spaceMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BlogSpaceDO> getSpacePage(BlogSpacePageReqVO pageReqVO) {
        return spaceMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BlogSpaceDO> getSpaceList(BlogSpaceExportReqVO exportReqVO) {
        return spaceMapper.selectList(exportReqVO);
    }

    @Override
    public void updateSpaceLikeNum(Long spaceId, Integer value) {
        validateSpaceExists(spaceId);
        BlogSpaceDO blogSpaceDO = spaceMapper.selectById(spaceId);
        blogSpaceDO.setLikeNum(blogSpaceDO.getLikeNum() + value);
        spaceMapper.updateById(blogSpaceDO);
    }

    @Override
    public void updateSpaceCommentNum(Long spaceId, Integer value) {
        validateSpaceExists(spaceId);
        BlogSpaceDO blogSpaceDO = spaceMapper.selectById(spaceId);
        blogSpaceDO.setCommentNum(blogSpaceDO.getCommentNum() + value);
        spaceMapper.updateById(blogSpaceDO);
    }

}
